﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Xsl;

namespace ReporterSystem
{
    internal class cifRequestHandler
    {
        // Methods
        public bool AcceptRequest(cifRequest cRequest, string strRequestFile)
        {
          clsProvision provision = new clsProvision();

            var started = provision.StartProvision();

            if (!started)
                return false;

           

           if (File.Exists(strRequestFile))
            {
                File.Delete(strRequestFile);
            }
            
            if(cRequest.Images)
            {
                //File.Delete();

                foreach (var file in Directory.GetFiles(cRequest.ImageTarget))
                {
                    File.Delete(file);
                }

                Directory.Delete(cRequest.ImageTarget);
                Directory.CreateDirectory(cRequest.ImageTarget);
            }

            cRequest.Generate(strRequestFile);

            if (!File.Exists(strRequestFile))
            {
                throw new Exception(Resources.IDS_FILE_CREATION_ERROR + " " + strRequestFile);
            }
            if (File.Exists(cRequest.ResultFile))
            {
                File.Delete(cRequest.ResultFile);
            }
            provision.SubmitCIFRequest(strRequestFile);

            if (!File.Exists(cRequest.ResultFile))
            {
                throw new Exception(Resources.IDS_FILE_CREATION_ERROR + " " + cRequest.ResultFile);
            }

            return true;
        }
    }
}
